import { Request, Response, NextFunction } from 'express';
import { getAdminCon } from '../admin/admin.service';
import { SignToken } from './auth.service';

/**
 * 用户登录
 */
export const login = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取数据
  const { user: {id, name} } = request.body;

  // 获取管理员 level
  let adminCon = await getAdminCon(request.body.user.id)

  //type: 1:admin
  const payload = { id, name, type: 1, level: adminCon[0].level };

  try {
    // 签发令牌
    const token = SignToken({ payload });

    // 做出响应
    response.send({id, name, token});
  } catch (error) {
    next(error);
  }
}

/**
 * 验证登录
 */
export const validate = (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  console.log(request.user);
  response.sendStatus(200);
}
